using System.Collections.Generic;
using SimpleJSON;

namespace Gj
{
    /**
     * 固定列表
     * 需要设置固定列表list和map
     * 子集可以绑定ShowHelper用来设置showItem和hideItem
     * 通过下发列表长度显示子集样式
     * ShowHelper不需要设定数据，由父集控制show状态
     * MultipleItemHelper
     */
    public class MultipleListHelper : NodeHelper
    {
        public List<NodeHelper> list = new();
        public BindDataType dataType = BindDataType.List;

        protected override BindDataType bindDataType => dataType;

        protected override void BindData(JSONArray array)
        {
            for (var i = 0; i < list.Count; i++)
                if (array[i] == null)
                    ItemHide(i, list[i]);
                else
                    ItemShow(i, list[i], array[i].AsObject);
        }

        protected override void BindData(JSONObject obj)
        {
            for (var i = 0; i < list.Count; i++)
                if (obj[list[i].key] == null)
                    ItemHide(list[i]);
                else
                    ItemShow(list[i], obj[list[i].key].AsObject);
        }

        protected void ItemShow(int i, NodeHelper obj, JSONObject json)
        {
            BindEvent(obj);
            obj.SetIndex(i);
            obj.SetData(true);
            obj.SetData(json);
        }

        protected void ItemHide(int i, NodeHelper obj)
        {
            BindEvent(obj);
            obj.SetIndex(i);
            obj.SetData(false);
            obj.SetData(new JSONObject());
        }

        protected void ItemShow(NodeHelper obj, JSONObject json)
        {
            BindEvent(obj);
            obj.SetData(true);
            obj.SetData(json);
        }

        protected void ItemHide(NodeHelper obj)
        {
            BindEvent(obj);
            obj.SetData(false);
            obj.SetData(new JSONObject());
        }
    }
}